﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Sherwood.SignOn.Server.Models.Repositories;
using Sherwood.SignOn.Server.Models.Configuration;
using Sherwood.SignOn.Server.Models.ViewModels;

namespace Sherwood.SignOn.Server.Models.Services
{
    public static class UserAccountHelper
    {

        public static bool IsAdministrator(string username)
        {
            if (Config.Settings.Administrators != null && Config.Settings.Administrators.Count > 0)
            {
                username = username.Trim().ToLower();
                foreach (AdministratorsElement administrator in Config.Settings.Administrators)
                {
                    if (administrator.Username.Equals(username, StringComparison.OrdinalIgnoreCase))
                        return true;
                }
            }
            return false;
        }

        public static bool IsGlobalAccount(string username)
        {
            if (Config.Settings.GlobalAccounts != null && Config.Settings.GlobalAccounts.Count > 0)
            {
                username = username.Trim().ToLower();
                foreach (GlobalAccountsElement globalAccount in Config.Settings.GlobalAccounts)
                {
                    if (globalAccount.Username.Equals(username, StringComparison.OrdinalIgnoreCase))
                        return true;
                }
            }
            return false;
        }


        public static bool IsWeakPassword(string username, string password)
        {
            bool isStrong = password != null && username != null && !password.Equals(username, StringComparison.OrdinalIgnoreCase) && password.Length > 3;

            if (isStrong && Config.Settings.WeakPasswords != null && Config.Settings.WeakPasswords.Count > 0)
            {
                password = password.Trim().ToLower();
                foreach (WeakPasswordsElement weakPassword in Config.Settings.WeakPasswords)
                {
                    if (weakPassword.Password.Equals(password, StringComparison.OrdinalIgnoreCase))
                        return true;
                }
            }
            return !isStrong;
        }

        public static bool IsWeakUsername(string username)
        {
            if (Config.Settings.WeakPasswords != null && Config.Settings.WeakPasswords.Count > 0)
            {
                username = username.Trim().ToLower();
                foreach (WeakPasswordsElement weakPassword in Config.Settings.WeakPasswords)
                {
                    if (weakPassword.Password.Equals(username, StringComparison.OrdinalIgnoreCase))
                        return true;
                }
            }
            return false;
        }


        /// <summary>
        /// Extension method which gets all the users roles and attributes (from all profile providers)
        /// </summary>
        /// <param name="userAccount"></param>
        /// <param name="requestClientCode"></param>
        /// <param name="roles"></param>
        /// <param name="attributes"></param>
        public static void GetProfile(this IUserAccount userAccount, string requestClientCode, out RoleInfo[] roles, out AttributeInfo[] attributes)
        {
            List<RoleInfo> allRoles = new List<RoleInfo>();
            List<AttributeInfo> allAttributes = new List<AttributeInfo>();

            if (userAccount.VerificationStatus == VerificationStatus.Verified && userAccount.IsEnabled)
            {

                foreach (IProfileProvider provider in Data.UserProfiles.ProfileProviders)
                {
                    RoleInfo[] providerRoles = new RoleInfo[0];
                    AttributeInfo[] providerAttributes = new AttributeInfo[0];
                    try
                    {
                        provider.GetRoles(userAccount, requestClientCode, out providerRoles, out providerAttributes);
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                    if (providerRoles.Length > 0)
                        allRoles.AddRange(providerRoles);
                    if (providerAttributes.Length > 0)
                        allAttributes.AddRange(providerAttributes);
                }
            }

            roles = allRoles.ToArray();
            attributes = allAttributes.ToArray();
        }


    }
}